package com.example.shangjinlieren.tabbar.fraction;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.ability.fraction.FractionManager;

import java.util.ArrayList;
import java.util.List;

public  class FractionUtil {
    private Integer currentSaveIndex=0;//当前正在显示的索引值
    private List<Fraction> fractions ;
    private FractionAbility ability;
    public FractionUtil(List<Fraction> fractions,FractionAbility ability,int resourceTableId) {
        this.fractions = fractions;
        this.ability=ability;
        System.out.println("ability.getContext()"+ability.getContext());
        initTabberPage(resourceTableId);
    }
    public void initTabberPage(int resourceTableId){

        getFractionManager()
                .startFractionScheduler()
                .add(resourceTableId,this.fractions.get(0))
                .add(resourceTableId,this.fractions.get(1))
                .submit();
    }
    /**
     * 获取Fraction管理器对象
     @return
     */
    public  FractionManager getFractionManager(){
//        Ability ability = this.abilitySlice.getAbility();
        if(this.ability instanceof FractionAbility){
            FractionAbility fractionAbility = (FractionAbility) this.ability;
            return fractionAbility.getFractionManager();
        }
        return null;
    }

    /**
     *
     * @param indexHide 隐藏的索引
     */
    public  void hide(Integer indexHide){
        getFractionManager()
        .startFractionScheduler()
        .hide(this.fractions.get(indexHide))
        .submit();
    }

    /**
     *
     * @param indexshow 显示点击的页面索引
     */
    public void show(Integer indexshow){
        //隐藏当前显示的tabber页面
        System.out.println("this.currentSaveIndex="+this.currentSaveIndex+"index="+indexshow);
        System.out.println("this.fractions"+this.fractions.size());
        hide(this.currentSaveIndex);
        //显示将要跳转的tabber页面
        getFractionManager()
                .startFractionScheduler()
                .show(this.fractions.get(indexshow))
                .submit();
        //讲新页面赋给当前索引值
        this.currentSaveIndex=indexshow;
    }
}
